<?php

namespace app\admin\model\information;
use think\Model;
use think\Log;
use app\admin\model\information\Oaflow15s;
use app\common\model\Inventory;
use app\admin\model\information\U8department;
use app\admin\model\inventory\Material;
use app\admin\model\information\Oaflowrun;

class Oaflow15 extends Model
{
    protected $connection = "database.forth";
    protected $table = "zzzz_flow_data_15";
    
    public function getOaData(){
        $start_date = config('site.oa_syn_start');
        $end_date = config('site.oa_syn_end');
        $syn_num = config('site.oa_syn_num');
        
        if($syn_num&&is_numeric($syn_num)){
            $syn_num--;
            $end_date = date('Y-m-d',time());
            $start_date = $date = date('Y-m-d', strtotime("-{$syn_num} days"));
        }
        
        if($end_date){
            $end_date.=' 23:59:59';
        }
        
        $heads = self::where([
            'DATA_26' => ['NEQ','NOT NULL'],
            'DATA_27' => ['BETWEEN',[$start_date,$end_date]],
        ])
        ->select();
        
        if($heads){
            foreach ($heads as $head){
                //查询oa流程表，获得oa流水号
                $oa_order_code = Oaflowrun::where('run_id',$head->run_id)->value('run_seq_strip_tags');
                
                $check = Material::where('material_code',$oa_order_code)->find();//$head->DATA_1
                if(!$check){
                    //明细
                    $bodys = Oaflow15s::where([
                        'run_id' => $head->run_id,
                        'DATA_10_8' => ['NEQ','BG']
                    ])
                    ->select();
                    
                    if($bodys){
                        //项目名，当部门名称
                        $u8_dpt_code ='';
                        if(!empty($head->DATA_34)){
                            $u8_dpt_code = U8department::where('cDepName',$head->DATA_34)->value('cDepCode');
                        }
                        
                        db('material')->insert([
                            'material_code'=>$oa_order_code,//$head->DATA_1,
                            'order_date'=>substr($head->DATA_27, 0,10),
                            'crdname' => '生产领用',
                            'crdcode' => '22',
                            //'sh_time'=>'',
                            'status'=>1,
                            'source'=>1,
                            'fitemss'=>$head->DATA_16_TEXT,
                            'fitemssname'=>$head->DATA_34,
                            'createtime'=>$head->DATA_19,
                            'fitemsbs' => $head->DATA_33_TEXT,
                            //'cUser_Id' => $oaData['userInfo']['user_accounts'],
                            //'cUser_Name' => $oaData['userInfo']['user_name'],
                            'cUser_Id' => $head['DATA_5'],
                            'cUser_Name' => $head['DATA_5_TEXT'],
                            'cdepname' => $head['DATA_3'],
                            'cdepcode' => $u8_dpt_code
                        ]);
                        
                        foreach ($bodys as $k=>$body){
                            $inventory_number = $body->DATA_10_6;
                            
                            if(substr($inventory_number, 0,2)!='08'&&substr($inventory_number, 0,2)!='04'){
                                //只取五金和固定资产
                                continue;
                            }
                            if(substr($inventory_number, 0,4)=='0810'){
                                //化学类的，过滤掉
                                continue;
                            }
                            
                            $materialListData = [
                                'auto_id' => '',
                                'material_code' => $oa_order_code,//$head->DATA_1,
                                'row_id' => $k+1,
                                'inv_id' => db('inventory')->where(['number'=>$inventory_number])->value('id'),
                                'inventory_number' => $inventory_number,
                                'quantity' => $body->DATA_10_3,
                                'createtime'=>date('Y-m-d H:i:s'),
                                'remark' => $body->DATA_10_4
                            ];
                            
                            //获得推荐路径，在物料里
                            $wh_sh_arr = Inventory::getTjWhShByInvNumber($inventory_number);
                            if($wh_sh_arr){
                                $materialListData['warehouse_id'] = $wh_sh_arr['warehouse_id'];
                                $materialListData['tj_shelves_id'] = $wh_sh_arr['shelves_id'];//推荐货架id，查询最新库存记录
                            }
                            
                            db('material_list')->insert($materialListData);
                        }
                    }
                }else{
                    //针对外发过来的，没有带部门id的，再次获取,项目名当部门
                    if(!$check->cdepcode&&$check->fitemssname){
                        $check->cdepcode = U8department::where('cDepName',$check->fitemssname)->value('cDepCode','');
                        $check->save();
                    }
                }
            }
        }else{
            exception('暂无最新oa数据');
        }
        
        Log::info(['start_date'=>$start_date,'end_date'=>$end_date],'同步oa领料单');
    }
}
